package by.dvrudenko.avtobaza.services.impl;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.support.DaoSupport;
import org.springframework.stereotype.Service;

import by.dvrudenko.avtobaza.dataaccess.DriverDao;
import by.dvrudenko.avtobaza.dataaccess.RouteDao;
import by.dvrudenko.avtobaza.dataaccess.en.Restriction;
import by.dvrudenko.avtobaza.datamodel.Driver;
import by.dvrudenko.avtobaza.datamodel.Driver_;
import by.dvrudenko.avtobaza.datamodel.Route;
import by.dvrudenko.avtobaza.datamodel.Route_;
import by.dvrudenko.avtobaza.services.DriverService;

@Service
public class DriverServiceImpl implements DriverService{
	private static final Logger LOGGER = LoggerFactory.getLogger(DriverServiceImpl.class);
	
	@Inject
	private DriverDao daoDriver;
	
	@Inject 
	private RouteDao daoRoute;
	
	@Value(value="${rootImagesFolfer}")
	private String rootFolder;
		
	@PostConstruct
	private void init() {
		LOGGER.info("Instance of DriverService is created. Class is: {}",
				getClass().getName());
	}
	
	@Override
	public Driver get(Long id) {
		Driver entity = daoDriver.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Driver driver) {
		if (driver.getId() == null) {
			LOGGER.debug("Save new: {driver}", driver);
			daoDriver.insert(driver);
		} else {
			LOGGER.debug("Update: {driver}", driver);
			daoDriver.update(driver);
		}
	}

	@Override
	public void delete(Driver driver) {
		
		List<Route> routeDriver = daoRoute.getRouteByAtr(Route_.driver, driver.getId());
		
		for (int i=0;i<routeDriver.size();i++){
			LOGGER.debug("Remove: {route}", routeDriver.get(i).getId());
			daoRoute.delete(routeDriver.get(i).getId());
		}
		
		
		LOGGER.debug("Remove: {driver}", driver);
		daoDriver.delete(driver.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all driver");
		daoDriver.deleteAll();
	}

	@Override
	public List<Driver> getAllDriver() {	
		return daoDriver.getAllDriver();
	}

	@Override
	public List<Driver> getAllDriverWork(Boolean work) {			
		return daoDriver.getAllByFieldRestriction(Driver_.work,work);
	}

/*	@Override
	public List<Driver> getAllDriverByExperience(Timestamp experience, Restriction restriction) {
		return dao.getAllByFieldGreaterOrLessThan(Driver_.experience, experience, restriction);
	}*/

	@Override
	public List<Driver> getAllDriverByAge(Timestamp date, Restriction restriction) {
		return daoDriver.getAllByFieldByDate(Driver_.birthday, date, restriction);
	}

	@Override
	public void changeDriverWorkStatus(Long id, Boolean bool) {
		Validate.notNull(bool, "Boolean value cannot be Null!");
		Driver driver = daoDriver.getById(id);
		// при завершении маршрута к-во выполненных заданий увеличивается на 1
		driver.setAcomplishRoutes(driver.getAcomplishRoutes()+1);
		driver.setWork(bool);
		daoDriver.update(driver);
	}	
	
	public Driver getRandomDriver(){		
		return daoDriver.randomEntity();
	}

	@Override // возвращает все маршруты по данному водителю
	public Integer getAcomplishRoutesByDriverId(Long id) {
		Validate.notNull(id, "Field id cannot be null");
		return daoRoute.getAcomplishRoutesByDriverId(id);
	}

	@Override
	public List<Driver> getAllDrivers(SingularAttribute<Driver, ?> attr,
			boolean ascending, int startRecord, int pageSize) {
		return daoDriver.getAllDriver(attr, ascending, startRecord, pageSize);
	}

	@Override
	public Long getCount() {
		return daoDriver.getCount();
	}
	
	public String getRootFolder() {
		return rootFolder;
	}

	@Override
	public Driver getDriverByUserId(Long id) {			
		return daoDriver.getDriverByUserId(id);
	}

}
